package com.elinkway.infinitemovies.ui.activity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;

import com.elinkway.infinitemovies.ui.activity.play.PlayerUtils;
import com.elinkway.infinitemovies.utils.DialogUtil;
import com.elinkway.infinitemovies.utils.LogUtils;
import com.elinkway.infinitemovies.utils.NetWorkTypeUtils;
import com.elinkway.infinitemovies.utils.Utils;

public class BaseSecondaryActivity extends BaseActionBarActivity {
    private String TAG = "BaseSecondaryActivity";

    //网络变化广播接收
    private NetworkCheckReceiver mCheckReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    }

    /**
     * qinguoli
     * 注册监听手机网络广播 接收
     */
    protected void registerCheckNetwork() {
        mCheckReceiver = new NetworkCheckReceiver();
        IntentFilter intentfilter = new IntentFilter();
        intentfilter.addAction(PlayerUtils.CONNECTIVTY_CHANGE);
        intentfilter.addAction(Utils.SDCARD_NOSPACE_ACTION);
        this.registerReceiver(mCheckReceiver, intentfilter);
    }

    /**
     * qinguoli
     * 取消对网络变化的监听*
     */
    protected void unregisterCheckNetwork() {
        if (mCheckReceiver != null) {
            try {
                this.unregisterReceiver(mCheckReceiver);
            } catch (IllegalArgumentException e) {
                if (e.getMessage().contains("Receiver not registered")) {
                    LogUtils.e(TAG, "!!!!!!!!!!!!!!!!!ignore incorrect unregisterCheckNetwork!!!!!!!!!!!!!!!!!");
                    // Ignore this exception. This is exactly what is desired
                } else {
                    // unexpected, re-throw
                    throw e;
                }
            }
        }
    }

    /**
     * qinguoli
     * 网络广播监听*
     */
    class NetworkCheckReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent != null) {
                if (intent.getAction().equals(PlayerUtils.CONNECTIVTY_CHANGE)) {
                    //读取保存的之前网络状态
                    SharedPreferences priorNetState = getSharedPreferences("priornetstate",
                            Activity.MODE_PRIVATE);

                    int priorNetType = priorNetState.getInt("netstate", 2);

                    int nowNetType = NetWorkTypeUtils.checkNet(intent, priorNetType);


                    SharedPreferences.Editor editor = priorNetState.edit();

                    editor.putInt("netstate", nowNetType);
                    editor.commit();
                }
                if (intent.getAction().equals(Utils.SDCARD_NOSPACE_ACTION)) {
                    DialogUtil.getInstance(BaseSecondaryActivity.this).checkSDCardSpaceDialog();
                }


            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        registerCheckNetwork();
    }

    @Override
    protected void onStop() {
        super.onStop();
        unregisterCheckNetwork();
    }

    @Override
    public void dealWithUmeng() {
    }

    @Override
    public void dealWithVisitUmeng() {

    }

}
